package com.cxh.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.common.dao.BaseDao;
import com.common.entity.A_auctionMsg;
import com.cxh.dao.A_auctionMsgDao;

public class A_auctionMsgDaoImpl extends BaseDao<A_auctionMsg> implements A_auctionMsgDao {
	@Override
	/**
	 * ע�⣺ʹ��ʱmp��key����Ϊ���������
	 * ����Ϊ������Բ����������
	 * Integer auctionMsg_id, Integer goods_id, Integer customer_id,
	 * Double money,		  Integer markup_id,Date start_date,
	 * Integer timeRules_id,  Date end_date, 	Integer goodsState_id
	 */
	public List<A_auctionMsg> findByCondition(Map<String, Object> mp) {
		List<Object> pList = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer(" select * from a_auctionMsg where 1=1 ");
		if(!(mp==null||mp.size()<1))
			for(Entry<String, Object> et:mp.entrySet()){
				if(et.getKey()=="start_date")
					sb.append("and to_char(start_date,'yyyy-MM-dd')=? ");
				else
					sb.append("and "+et.getKey()+"=? ");
				pList.add(et.getValue());
			}
		return findBySql(sb.toString(), pList.toArray());
	}

	@Override
	public A_auctionMsg findById(int id) {
		String sql = " select * from a_auctionMsg where auctionMsg_id = ? ";
		Object[] objs = {id};
		List<A_auctionMsg> lg = findBySql(sql, objs);
		return (lg!=null&&lg.size()>0)?lg.get(0):null;
	}

	@Override
	/**
	 * ע�⣺ʹ��ʱmp��key����Ϊ���������
	 * ����Ϊ������Բ����������
	 * Integer auctionMsg_id, Integer goods_id, Integer customer_id,
	 * Double money,		  Integer markup_id,Date start_date,
	 * Integer timeRules_id,  Date end_date, 	Integer goodsState_id
	 */
	public int updateA_auctionMsg(Map<String, Object> mp) {
		List<Object> pList = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer(" update a_auctionMsg set ");
		if(mp!=null&&mp.size()>0){
			for(Entry<String, Object> et:mp.entrySet()){
				if(!(et.getKey()=="auctionMsg_id")){
					sb.append(et.getKey()+" = ?,");
					pList.add(et.getValue());
				}
			}
			String sb1 = sb.toString();
			sb1=sb1.substring(0, sb1.length()-1);
			sb = new StringBuffer(sb1);
			for(Entry<String, Object> et:mp.entrySet()){
				if(et.getKey()=="auctionMsg_id"){
					sb.append("where auctionMsg_id = ?");
					pList.add(et.getValue());
				}
			}
		}
		/*String sql = "update a_auctionMsg set goods_id=?, customer_id=?, money=?, markup_id=?,"+
				" start_date = ?, timeRules_id=?, end_date=?, goodsState_id=? where auctionMsg_id=?";
		Object[] objs = {mp.get("goods_id"),mp.get("customer_id"),mp.get("money"),mp.get("markup_id"),
				mp.get("start_date"),mp.get("timeRules_id"),mp.get("end_date"),mp.get("goodsState_id"),
				mp.get("auctionMsg_id")};*/
		return executeUpdate(sb.toString(), pList.toArray());
	}

	@Override
	public int deleteA_auctionMsg(int id) {
		String sql = "delete from a_auctionMsg where auctionMsg_id = ?";
		Object[] objs = {id};
		return executeUpdate(sql, objs);
	}

	@Override
	/**
	 * ע�⣺ʹ��ʱmp��key����Ϊ���������
	 * ����Ϊ������Բ����������
	 * Integer auctionMsg_id, Integer goods_id, Integer customer_id,
	 * Double money,		  Integer markup_id,Date start_date,
	 * Integer timeRules_id,  Date end_date, 	Integer goodsState_id
	 */
	public int addA_auctionMsg(Map<String, Object> mp) {
		String sql = "insert into a_auctionMsg values(seq_a_auctionmsg.nextval,?,?,?,?,?,?,?,?)";
		Object[] objs = {mp.get("goods_id"),mp.get("customer_id"),mp.get("money"),mp.get("markup_id"),
				mp.get("start_date"),mp.get("timeRules_id"),mp.get("end_date"),mp.get("goodsState_id")};
		return executeUpdate(sql, objs);
	}

	@Override
	public A_auctionMsg getEntity(ResultSet rs) {
		A_auctionMsg am = new A_auctionMsg();
		try {
			am.setAuctionMsg_id(rs.getInt("auctionMsg_id"));
			am.setCustomer_id(rs.getInt("customer_id"));
			am.setEnd_date(rs.getDate("end_date"));
			am.setGoods_id(rs.getInt("goods_id"));
			am.setGoodsState_id(rs.getInt("goodsState_id"));
			am.setMarkup_id(rs.getInt("markup_id"));
			am.setMoney(rs.getDouble("money"));
			am.setStart_date(rs.getDate("start_date"));
			am.setTimeRules_id(rs.getInt("timeRules_id"));
			return am;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public int countAuctionMsg() {
		String sql = "a_auctionMsg";
		return getCount(sql, null);
	}
}
